/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.src.uniandes.ecos.tsp.creditscore.jpa;

import com.src.uniandes.ecos.tsp.creditscore.entity.EstadoReporte;
import com.src.uniandes.ecos.tsp.creditscore.jpa.exceptions.NonexistentEntityException;
import com.src.uniandes.ecos.tsp.creditscore.jpa.exceptions.PreexistingEntityException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.src.uniandes.ecos.tsp.creditscore.entity.HistorialCrediticio;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author AndhyMao
 */
public class EstadoReporteJpaController {

    public EstadoReporteJpaController() {
        emf = Persistence.createEntityManagerFactory("com.src.uniandes.ecos.tsp.creditscore_CreditScoreEntities_jar_1.0-SNAPSHOTPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EstadoReporte estadoReporte) throws PreexistingEntityException, Exception {
        if (estadoReporte.getHistorialCrediticioList() == null) {
            estadoReporte.setHistorialCrediticioList(new ArrayList<HistorialCrediticio>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<HistorialCrediticio> attachedHistorialCrediticioList = new ArrayList<HistorialCrediticio>();
            for (HistorialCrediticio historialCrediticioListHistorialCrediticioToAttach : estadoReporte.getHistorialCrediticioList()) {
                historialCrediticioListHistorialCrediticioToAttach = em.getReference(historialCrediticioListHistorialCrediticioToAttach.getClass(), historialCrediticioListHistorialCrediticioToAttach.getHcrCodigo());
                attachedHistorialCrediticioList.add(historialCrediticioListHistorialCrediticioToAttach);
            }
            estadoReporte.setHistorialCrediticioList(attachedHistorialCrediticioList);
            em.persist(estadoReporte);
            for (HistorialCrediticio historialCrediticioListHistorialCrediticio : estadoReporte.getHistorialCrediticioList()) {
                EstadoReporte oldEreCodigoOfHistorialCrediticioListHistorialCrediticio = historialCrediticioListHistorialCrediticio.getEreCodigo();
                historialCrediticioListHistorialCrediticio.setEreCodigo(estadoReporte);
                historialCrediticioListHistorialCrediticio = em.merge(historialCrediticioListHistorialCrediticio);
                if (oldEreCodigoOfHistorialCrediticioListHistorialCrediticio != null) {
                    oldEreCodigoOfHistorialCrediticioListHistorialCrediticio.getHistorialCrediticioList().remove(historialCrediticioListHistorialCrediticio);
                    oldEreCodigoOfHistorialCrediticioListHistorialCrediticio = em.merge(oldEreCodigoOfHistorialCrediticioListHistorialCrediticio);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEstadoReporte(estadoReporte.getEreCodigo()) != null) {
                throw new PreexistingEntityException("EstadoReporte " + estadoReporte + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EstadoReporte estadoReporte) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EstadoReporte persistentEstadoReporte = em.find(EstadoReporte.class, estadoReporte.getEreCodigo());
            List<HistorialCrediticio> historialCrediticioListOld = persistentEstadoReporte.getHistorialCrediticioList();
            List<HistorialCrediticio> historialCrediticioListNew = estadoReporte.getHistorialCrediticioList();
            List<HistorialCrediticio> attachedHistorialCrediticioListNew = new ArrayList<HistorialCrediticio>();
            for (HistorialCrediticio historialCrediticioListNewHistorialCrediticioToAttach : historialCrediticioListNew) {
                historialCrediticioListNewHistorialCrediticioToAttach = em.getReference(historialCrediticioListNewHistorialCrediticioToAttach.getClass(), historialCrediticioListNewHistorialCrediticioToAttach.getHcrCodigo());
                attachedHistorialCrediticioListNew.add(historialCrediticioListNewHistorialCrediticioToAttach);
            }
            historialCrediticioListNew = attachedHistorialCrediticioListNew;
            estadoReporte.setHistorialCrediticioList(historialCrediticioListNew);
            estadoReporte = em.merge(estadoReporte);
            for (HistorialCrediticio historialCrediticioListOldHistorialCrediticio : historialCrediticioListOld) {
                if (!historialCrediticioListNew.contains(historialCrediticioListOldHistorialCrediticio)) {
                    historialCrediticioListOldHistorialCrediticio.setEreCodigo(null);
                    historialCrediticioListOldHistorialCrediticio = em.merge(historialCrediticioListOldHistorialCrediticio);
                }
            }
            for (HistorialCrediticio historialCrediticioListNewHistorialCrediticio : historialCrediticioListNew) {
                if (!historialCrediticioListOld.contains(historialCrediticioListNewHistorialCrediticio)) {
                    EstadoReporte oldEreCodigoOfHistorialCrediticioListNewHistorialCrediticio = historialCrediticioListNewHistorialCrediticio.getEreCodigo();
                    historialCrediticioListNewHistorialCrediticio.setEreCodigo(estadoReporte);
                    historialCrediticioListNewHistorialCrediticio = em.merge(historialCrediticioListNewHistorialCrediticio);
                    if (oldEreCodigoOfHistorialCrediticioListNewHistorialCrediticio != null && !oldEreCodigoOfHistorialCrediticioListNewHistorialCrediticio.equals(estadoReporte)) {
                        oldEreCodigoOfHistorialCrediticioListNewHistorialCrediticio.getHistorialCrediticioList().remove(historialCrediticioListNewHistorialCrediticio);
                        oldEreCodigoOfHistorialCrediticioListNewHistorialCrediticio = em.merge(oldEreCodigoOfHistorialCrediticioListNewHistorialCrediticio);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = estadoReporte.getEreCodigo();
                if (findEstadoReporte(id) == null) {
                    throw new NonexistentEntityException("The estadoReporte with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EstadoReporte estadoReporte;
            try {
                estadoReporte = em.getReference(EstadoReporte.class, id);
                estadoReporte.getEreCodigo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The estadoReporte with id " + id + " no longer exists.", enfe);
            }
            List<HistorialCrediticio> historialCrediticioList = estadoReporte.getHistorialCrediticioList();
            for (HistorialCrediticio historialCrediticioListHistorialCrediticio : historialCrediticioList) {
                historialCrediticioListHistorialCrediticio.setEreCodigo(null);
                historialCrediticioListHistorialCrediticio = em.merge(historialCrediticioListHistorialCrediticio);
            }
            em.remove(estadoReporte);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<EstadoReporte> findEstadoReporteEntities() {
        return findEstadoReporteEntities(true, -1, -1);
    }

    public List<EstadoReporte> findEstadoReporteEntities(int maxResults, int firstResult) {
        return findEstadoReporteEntities(false, maxResults, firstResult);
    }

    private List<EstadoReporte> findEstadoReporteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from EstadoReporte as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EstadoReporte findEstadoReporte(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EstadoReporte.class, id);
        } finally {
            em.close();
        }
    }

    public int getEstadoReporteCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from EstadoReporte as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
